﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


/// <summary>
///BirthdyRemind 的摘要说明
/// </summary>
public class BirthdayRemind
{
	public BirthdayRemind()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    private SqlConnection CreateConn()
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
        SqlConnection conn = new SqlConnection(connStr);

        return conn;
    }

    public ArrayList EmployeeNameList(int permissionsID, int departmentID)
    {
        ArrayList employeeNameList = new ArrayList();
        string cmdStr;
        DataSet ds;

        if (permissionsID == 3)//物业经理
        {
            cmdStr = "select * from tb_EmployeeInfo where State=1";
            ds = getDs(cmdStr);

            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr=ds.Tables [0].Rows[i];

                    EmployeeInfo emoloyeeInfo = getWillBirtydayName(dr);
                    if (emoloyeeInfo != null)
                    {
                        employeeNameList.Add(emoloyeeInfo);
                    }
                }
            }
        }
        else if (permissionsID == 4)//物业服务中心经理
        {
            cmdStr = "select ID from tb_Department where FID='" + departmentID + "'";
            DataSet departmentIDds = getDs(cmdStr);//获得部门下面的子部门的列表

            if (departmentIDds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < departmentIDds.Tables[0].Rows.Count; i++)
                {
                    DataRow departmentIDRow = departmentIDds.Tables[0].Rows[i];
                    int sonDepartmentID = Convert.ToInt32(departmentIDRow[0].ToString());

                    cmdStr = "select * from tb_EmployeeInfo where Department='" + sonDepartmentID + "' and State=1"; //查找子部门的员工信息
                    ds = getDs(cmdStr);

                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        for (int n = 0; n < ds.Tables[0].Rows.Count; n++)
                        {
                            DataRow dr = ds.Tables[0].Rows[n];

                            EmployeeInfo emoloyeeInfo = getWillBirtydayName(dr);
                            if (emoloyeeInfo != null)
                            {
                                employeeNameList.Add(emoloyeeInfo);
                            }
                        }
                    }
                }
            }
        }
        else if (permissionsID == 2 || permissionsID == 5)//人事部或部门主管
        {
            cmdStr = "select * from tb_EmployeeInfo where Department='" + departmentID + "' and State=1";
            ds = getDs(cmdStr);

            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];

                    EmployeeInfo emoloyeeInfo = getWillBirtydayName(dr);
                    if (emoloyeeInfo != null)
                    {
                        employeeNameList.Add(emoloyeeInfo);
                    }
                }
            }
        }

        return employeeNameList;
    }

    private DataSet getDs(string cmdStr)
    {
        SqlConnection conn = CreateConn();
        DataSet ds = new DataSet();

        try
        {
            SqlCommand comm = new SqlCommand(cmdStr, conn);

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = comm;
            da.Fill(ds);
        }
        catch (Exception ex)
        {
            conn.Close();
        }

        return ds;
    }


    /// <summary>
    /// 获得将要生日员工的信息
    /// </summary>
    /// <param name="dr">包含员工信息的行</param>
    /// <returns></returns>
    private EmployeeInfo getWillBirtydayName(DataRow dr)
    {
        EmployeeInfo name = new EmployeeInfo();

        string employeeIDCard = dr["IDCard"].ToString(); 
        string birthday = employeeIDCard.Substring(10, 4);
        birthday = birthday.Insert(2, "/");
        string year = DateTime.Now.ToString("yyyy");
        birthday = year + "/" + birthday;

        //DateTime Birthday = Convert.ToDateTime(birthday);
        DateTime Birthday = DateTime.Parse(birthday);
        /*if (Birthday.Month == DateTime.Now.Month)
        {
            //TimeSpan Ts = DateTime.Now.Subtract(Birthday);
            TimeSpan Ts = Birthday.Subtract(DateTime.Now);

            if (Ts.Days <= 1)
            {
                name.Name = dr["Name"].ToString();
                name.Department = dr["Department"].ToString();
            }
        }
        else
        {
            name = null;
        }*/

        TimeSpan Ts = Birthday.Subtract(DateTime.Now);
        if (Ts.Days > 0 && Ts.Days <= 1)
        {
            name.Name = dr["Name"].ToString();
            name.Department = dr["Department"].ToString();
            name.IDCard = dr["IDCard"].ToString();
            name.TelePhone = dr["TelePhone"].ToString();
        }
        else
        {
            name = null;
        }

        return name;
    }

}